package com.liu.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 使用字符流
 * @author liuchengjie
 */
@SuppressWarnings({"all"})
public class SocketTcpServer03 {
    public static void main(String[] args) throws IOException {
        //在本机9999端口监听，等待链接,要求端口没被占用
        ServerSocket serverSocket = new ServerSocket(9999);
        //当有客户端链接时，会返回对象socket,没有链接，会阻塞
        Socket socket = serverSocket.accept();
        System.out.println("socket的运行信息");
        System.out.println(socket.getClass());

        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s = bufferedReader.readLine();
        System.out.println(s);
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello,client");
        bufferedWriter.newLine();
        bufferedWriter.flush();
        //结束标记
        socket.shutdownOutput();
        inputStream.close();
        socket.close();
        serverSocket.close();
        outputStream.close();
        bufferedWriter.close();
    }
}
